package com.zhaopin.valatile;

/**
 * @author Administrator
 * @create 2021-02-26 11:38
 */
public class VolatileThread extends Thread {
    private final VolatileData volatileData;

    public VolatileThread(VolatileData volatileData) {
        this.volatileData = volatileData;
    }

    /**
     * 调用VolatileData类中的两个方法，进行取值和自增操作
     */
    @Override
    public void run() {
        int oldValue = volatileData.getCounter();
        System.out.println("[Thread " + Thread.currentThread().getId() + "]: Old value = " + oldValue);
        volatileData.increaseCounter();
        int newValue = volatileData.getCounter();
        System.out.println("[Thread " + Thread.currentThread().getId() + "]: New value = " + newValue);
    }
}
